<template>
  <div class="seller-application-container">
    <div class="search-form">
      <el-form :inline="true" :model="searchForm" ref="searchFormRef">
        <el-form-item label="申请人" prop="name">
          <el-input v-model="searchForm.name" placeholder="请输入申请人名称"></el-input>
        </el-form-item>
        <el-form-item label="审核状态" prop="status">
          <el-select v-model="searchForm.status" placeholder="请选择状态" clearable>
            <el-option label="待审核" :value="0"></el-option>
            <el-option label="已通过" :value="1"></el-option>
            <el-option label="已拒绝" :value="2"></el-option>
          </el-select>
        </el-form-item>
        <el-form-item>
          <el-button type="primary" @click="handleSearch">搜索</el-button>
          <el-button @click="resetSearch">重置</el-button>
        </el-form-item>
      </el-form>
    </div>

    <el-table :data="tableData" border style="width: 100%" v-loading="loading">
      <el-table-column type="index" label="序号" width="60" align="center"></el-table-column>
      <el-table-column prop="username" label="用户名" min-width="120"></el-table-column>
      <el-table-column prop="name" label="真实姓名" min-width="120"></el-table-column>
      <el-table-column prop="idNumber" label="身份证号" min-width="180"></el-table-column>
      <el-table-column prop="businessLicense" label="营业执照" min-width="180">
        <template #default="scope">
          <el-image v-if="scope.row.businessLicense" style="width: 100px; height: 100px"
            :src="scope.row.businessLicense" :preview-src-list="[scope.row.businessLicense]" fit="cover" />
          <span v-else>暂无图片</span>
        </template>
      </el-table-column>
      <el-table-column prop="status" label="审核状态" width="100">
        <template #default="scope">
          <el-tag :type="scope.row.status === 0 ? 'info' : scope.row.status === 1 ? 'success' : 'danger'">
            {{ scope.row.status === 0 ? '待审核' : scope.row.status === 1 ? '已通过' : '已拒绝' }}
          </el-tag>
        </template>
      </el-table-column>
      <el-table-column label="操作" width="200" align="center">
        <template #default="scope">
          <el-button v-if="scope.row.status === 0" type="success" size="small"
            @click="handleApprove(scope.row, 1)">通过</el-button>
          <el-button v-if="scope.row.status === 0" type="danger" size="small"
            @click="handleApprove(scope.row, 2)">拒绝</el-button>
          <span v-else>已处理</span>
        </template>
      </el-table-column>
    </el-table>

    <div class="pagination-container">
      <el-pagination @size-change="handleSizeChange" @current-change="handleCurrentChange"
        :current-page="searchForm.page" :page-sizes="[10, 20, 50, 100]" :page-size="searchForm.pageSize"
        layout="total, sizes, prev, pager, next, jumper" :total="total"></el-pagination>
    </div>
  </div>
</template>

<script setup>
import { ref, reactive, onMounted } from 'vue'
import { getSellerApplicationList, approveSellerApplication } from '@/api/seller'
import { ElMessage, ElMessageBox } from 'element-plus'

// 搜索表单
const searchForm = reactive({
  name: '',
  status: '',
  page: 1,
  pageSize: 10
})

const searchFormRef = ref(null)
const loading = ref(false)
const tableData = ref([])
const total = ref(0)

// 加载卖家申请列表
const loadSellerApplications = async () => {
  loading.value = true
  try {
    const res = await getSellerApplicationList(searchForm)
    console.log('卖家申请列表数据:', res)
    if (res && res.records) {
      tableData.value = res.records
      total.value = res.total
    } else if (res && Array.isArray(res)) {
      tableData.value = res
      total.value = res.length
    } else {
      tableData.value = []
      total.value = 0
      console.error('返回的数据格式不符合预期:', res)
    }
  } catch (error) {
    console.error('加载卖家申请列表失败:', error)
    ElMessage.error('加载卖家申请列表失败')
    tableData.value = []
    total.value = 0
  } finally {
    loading.value = false
  }
}

// 搜索
const handleSearch = () => {
  searchForm.page = 1
  loadSellerApplications()
}

// 重置搜索
const resetSearch = () => {
  searchFormRef.value.resetFields()
  searchForm.page = 1
  loadSellerApplications()
}

// 审核卖家申请
const handleApprove = (row, status) => {
  const statusText = status === 1 ? '通过' : '拒绝'

  ElMessageBox.confirm(`确定要${statusText}该卖家申请吗?`, '提示', {
    confirmButtonText: '确定',
    cancelButtonText: '取消',
    type: 'warning'
  }).then(async () => {
    try {
      console.log(`开始审核卖家申请，ID: ${row.id}, 状态: ${status}`)
      await approveSellerApplication(status, row.id)
      ElMessage.success(`卖家申请已${statusText}`)
      loadSellerApplications() // 重新加载列表
    } catch (error) {
      console.error(`审核操作失败:`, error)
      ElMessage.error(`操作失败: ${error.message || '未知错误'}`)
    }
  }).catch(() => { })
}

// 分页处理
const handleSizeChange = (val) => {
  searchForm.pageSize = val
  loadSellerApplications()
}

const handleCurrentChange = (val) => {
  searchForm.page = val
  loadSellerApplications()
}

// 页面加载时获取数据
onMounted(() => {
  loadSellerApplications()
})
</script>

<style scoped>
.seller-application-container {
  padding: 20px;
}

.search-form {
  margin-bottom: 20px;
  background-color: #fff;
  padding: 20px;
  border-radius: 4px;
  box-shadow: 0 2px 12px 0 rgba(0, 0, 0, 0.1);
}

.pagination-container {
  margin-top: 20px;
  text-align: right;
}
</style>